﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NextInventory.Data;
using NextInventory.Model.Menufacturer;

namespace NextInventory.Business.EntityRepositories
{
    public interface IMenufacturerRepository : IBaseRepository
    {
        #region Manufacturer

        MenufacturerModel GetMenufacturerByID(int Id);
        List<MenufacturerModel> SearchMenufacturer();
        List<MenufacturerModel> GetMenufacturerByModelID(int Id);

        bool AddMenufacturer(MenufacturerModel model);
        bool UpdateMenufacturer(MenufacturerModel model);
        bool DeleteMenufacturer(int Id);

        #endregion Menufacturer

    }
    public class MenufacturerRepository : BaseRepository, IMenufacturerRepository
    {
        NextInventoryContext context = new NextInventoryContext();

        #region Menufacturer

        public MenufacturerModel GetMenufacturerByID(int Id)
        {
            return (
                from m in context.Menufacturers
                select new MenufacturerModel
                {
                    MenufacturerId = m.MenufacturerId,
                    Name = m.Name,
                    Phone = m.Phone,
                    Email = m.Email,
                    Address = m.Address,
                    City = m.City,
                    Country = m.Country
                }
                ).FirstOrDefault(m => m.MenufacturerId == Id);
        }

        public List<MenufacturerModel> SearchMenufacturer()
        {
            List<MenufacturerModel> menufacturerList = new List<MenufacturerModel>();

            menufacturerList = (from menufacturer in context.Menufacturers
                                select new MenufacturerModel
                                {
                                    MenufacturerId = menufacturer.MenufacturerId,
                                    Name = menufacturer.Name,
                                    Phone = menufacturer.Phone,
                                    Email = menufacturer.Email,
                                    Address = menufacturer.Address,
                                    City = menufacturer.City,
                                    Country = menufacturer.Country
                                }).ToList();

            return menufacturerList;
        }

        public List<MenufacturerModel> GetMenufacturerByModelID(int Id)
        {
            throw new NotImplementedException();
        }

        public bool AddMenufacturer(MenufacturerModel model)
        {
            try
            {
                Menufacturer menufacturer = new Menufacturer()
                {
                    Name = model.Name,
                    Phone = model.Phone,
                    Email = model.Email,
                    Address = model.Address,
                    City = model.City,
                    Country = model.Country

                };

                context.Menufacturers.Add(menufacturer);
                context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
                throw ex;
            }
        }

        public bool UpdateMenufacturer(MenufacturerModel model)
        {
            Menufacturer menufacturer = context.Menufacturers.FirstOrDefault(m => m.MenufacturerId == model.MenufacturerId);
            menufacturer.Name = model.Name;
            menufacturer.Phone = model.Phone;
            menufacturer.Email = model.Email;
            menufacturer.Address = model.Address;
            menufacturer.City = model.City;
            menufacturer.Country = model.Country;
            context.SaveChanges();
            return true;
        }

        public bool DeleteMenufacturer(int Id)
        {
            Menufacturer model = context.Menufacturers.FirstOrDefault(m => m.MenufacturerId == Id);
            context.Menufacturers.Remove(model);
            context.SaveChanges();
            return true;
        }

        #endregion Menufacturer

    }
}
